package com.sgs.studentssm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sgs.common.config.SgsConfig;
import com.sgs.common.core.domain.model.LoginUser;
import com.sgs.common.utils.file.FileUploadUtils;
import com.sgs.common.utils.file.MimeTypeUtils;
import com.sgs.common.utils.poi.ExcelUtil;
import com.sgs.studentssm.domain.StuData;
import com.sgs.studentssm.service.IStuDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sgs.common.annotation.Log;
import com.sgs.common.core.controller.BaseController;
import com.sgs.common.core.domain.AjaxResult;
import com.sgs.common.enums.BusinessType;

import com.sgs.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import com.sgs.framework.web.service.TokenService;

/**
 * 学生个人资料Controller
 *
 * @author ruoyi
 * @date 2022-10-03
 */
@RestController
@RequestMapping("/studentssm/studata")
public class StuDataController extends BaseController
{
    @Autowired
    private IStuDataService stuDataService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询学生个人资料列表
     */
    @PreAuthorize("@ss.hasPermi('system:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(StuData stuData)
    {
        startPage();
        List<StuData> list = stuDataService.selectStuDataList(stuData);
        return getDataTable(list);
    }

    /**
     * 导出学生个人资料列表
     */
    @PreAuthorize("@ss.hasPermi('system:data:export')")
    @Log(title = "学生个人资料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StuData stuData)
    {
        List<StuData> list = stuDataService.selectStuDataList(stuData);
        ExcelUtil<StuData> util = new ExcelUtil<StuData>(StuData.class);
        util.exportExcel(response, list, "学生个人资料数据");
    }

    /**
     * 获取学生个人资料详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:data:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(stuDataService.selectStuDataById(id));
    }

    /**
     * 新增学生个人资料
     */
    @PreAuthorize("@ss.hasPermi('system:data:add')")
    @Log(title = "学生个人资料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StuData stuData)
    {
        return toAjax(stuDataService.insertStuData(stuData));
    }

    /**
     * 修改学生个人资料
     */
    @PreAuthorize("@ss.hasPermi('system:data:edit')")
    @Log(title = "学生个人资料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StuData stuData)
    {
        StuData stuData1 = new StuData();
        stuData1.setStuNumber(stuData.getStuNumber());
        if (stuDataService.selectStuDataList(stuData1).size()==0){
            return toAjax(stuDataService.insertStuData(stuData));
        }
        return toAjax(stuDataService.updateStuData(stuData));
    }

    /**
     * 删除学生个人资料
     */
    @PreAuthorize("@ss.hasPermi('system:data:remove')")
    @Log(title = "学生个人资料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stuDataService.deleteStuDataByIds(ids));
    }

    /**
     * 学生证件照上传
     */
    @Log(title = "学生证件照上传", businessType = BusinessType.UPDATE)
    @PostMapping("/stuphoto")
    public AjaxResult stuphoto(@RequestParam("photo") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            LoginUser loginUser = getLoginUser();
            String photo = FileUploadUtils.upload(SgsConfig.getUploadPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (stuDataService.updateStuPhoto(loginUser.getUsername(), photo))
            {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", photo);
                return ajax;
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }
    /**
     * 学生身份证正面上传
     */
    @Log(title = "学生身份证正面上传", businessType = BusinessType.UPDATE)
    @PostMapping("/stuidphoto1")
    public AjaxResult stuidphoto1(@RequestParam("idphoto1") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            LoginUser loginUser = getLoginUser();
            String stuidphoto1 = FileUploadUtils.upload(SgsConfig.getUploadPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (stuDataService.updateStuIdPhoto1(loginUser.getUsername(), stuidphoto1))
            {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", stuidphoto1);
                return ajax;
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }
    /**
     * 学生身份证反面上传
     */
    @Log(title = "学生身份证反面上传", businessType = BusinessType.UPDATE)
    @PostMapping("/stuidphoto2")
    public AjaxResult stuidphoto2(@RequestParam("idphoto2") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            LoginUser loginUser = getLoginUser();
            String stuidphoto2 = FileUploadUtils.upload(SgsConfig.getUploadPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (stuDataService.updateStuIdPhoto2(loginUser.getUsername(), stuidphoto2))
            {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", stuidphoto2);
                return ajax;
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }
}
